package springmvctest;

import java.util.Collection;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import restcrud.dao.DepartmentDao;
import restcrud.dao.EmployeeDao;
import restcrud.entites.Employee;

@Controller
public class SpingMvcT {

	@Autowired
	private EmployeeDao employeeDao;

	@Autowired
	private DepartmentDao departmentDao;

	
	
	/**
	 * 测试返回json数据 必须加入jackson包
	 * 
	 * @return Collection<Employee>
	 */
	@ResponseBody
	@RequestMapping("/testJson")
	public Collection<Employee> testJson() {
		return employeeDao.getAll();
	}

	/**
	 * 初始化数据绑定
	 * 
	 * @param webDataBinder
	 *            void
	 */
	@InitBinder
	public void testInitBinder(WebDataBinder webDataBinder) {
		// webDataBinder.setAllowedFields("username");
	}

	/**
	 * 测试自定义类型转换器
	 * 
	 * @param employee
	 * @return String
	 */
	@RequestMapping(value = "/testRmployeeConverter", method = RequestMethod.POST)
	public String testRmployeeConverter(@RequestParam("employee") Employee employee) {
		System.out.println("save: " + employee);
		employeeDao.save(employee);
		return "redirect:/emps";
	}
}
